<template>
  <div class="commission">
    <dt-search ref="search" @reset="resetFun">
      <Button type="primary" v-if="unlocking('0210020700')" @click="onExport">导出</Button>
    </dt-search>

    <dt-grid
      ref="table"
      :page="{
        current: filter.current || 1,
        rows: filter.rows || 50
      }"
      :onFetch="onFetch" />

    <payment-modal ref="payment" @success="onSuccess" />
    <invoice-modal ref="invoice" @success="onSuccess" />
    <writeoff-modal ref="writeoff" @success="onSuccess" />
    <confirm-modal ref="confirm" @success="onSuccess" />
    <invoice-receipt-modal ref="invoiceReceipt" @success="onSuccess" />
  </div>
</template>

<script>
  import Column from './opt/Column.js'
  import SearchOpt from './opt/SearchOpt.js'

  import paymentModal from './components/payment-modal.vue'
  import invoiceModal from './components/invoice-modal.vue'
  import writeoffModal from './components/writeoff-modal.vue'
  import confirmModal from './components/confirm-modal.vue'
  import invoiceReceiptModal from './components/invoice-receipt-modal.vue'

  import informationMgr from '@/biz/commission/information.js'

  let commissionId = null

  export default {
    components: {
      paymentModal,
      invoiceModal,
      writeoffModal,
      confirmModal,
      invoiceReceiptModal
    },
    data() {
      return {
        filter: {
          orgIds: null,
          housesId: null,
          housesName: null,
          companyId: null,
          commissionName: null,
          status: null,
          invoicingStatus: null,
          settlementStatus: null,
          voucherStatus: null,
          commissionMonth: null,
          commissionId: null
        },

      }
    },
    mounted() {
      this.$refs.search.init(this.$refs.table, new SearchOpt(), this.filter)
      if (this.$dt.memory.get('commission_filter')) {
        let data = this.$dt.memory.get('commission_filter')
        for (let key in data) {
          this.filter[key] = data[key]
        }
      }
      this.$refs.table.init(this, new Column(), {
        settlementStatuses: this.$route.params.settlementStatuses
      })
      commissionId = this.$route.params.businessId || commissionId
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        data.commissionId = commissionId
        this.filter = data
        return informationMgr.list(data)
      },
      // 重置待办任务的传参
      resetFun(){
        commissionId = null
      },
      // 佣金结算
      onSettlement(data) {
        this.$router.push({
          path: '/commission/settlement',
          query: {
            commissionId: data.commissionId
          }
        })
      },
      // 佣金明细
      onDetail(data) {
        this.$router.push({
          path: '/commission/detail',
          query: {
            commissionId: data.commissionId
          }
        })
      },
      // 回款信息
      onPayment(data) {
        this.$refs.payment.show(data)
      },
      // 开票
      onInvoice(data) {
        this.$refs.invoice.show(data)
      },
      // 核销
      onWriteoff(data) {
        let showTip = () =>
          this.$Modal.info({
            title: '提醒',
            content: '请先完成开票、回款、回单上传后，再进行核销操作',
            okText: '我知道了'
          })
        if (data.invoicingStatus == 'NO_INVOICE') {
          showTip()
        } else {
          informationMgr.writeOffBackList(data.housesId).then((res) => {
            if (res.length) {
              this.$refs.writeoff.show(data, res)
            } else {
              showTip()
            }
          })
        }
      },
      // 确认凭证
      onConfirm(data) {
        this.$refs.confirm.show(data)
      },
      onInvoiceReceipt(data) {
        this.$refs.invoiceReceipt.show(data)
      },
      // 导出
      onExport() {
        informationMgr.exportData(this.filter)
      },
      onSuccess() {
        this.$refs.table.fetch()
      }
    },
    beforeRouteLeave(to, from, next) {
      if (to.name == 'commissionSettlement' || to.name == 'commissionDetail') {
        this.$dt.memory.set('commission_filter', this.filter)
      } else {
        this.$dt.memory.remove('commission_filter')
      }
      next()
    }
  }
</script>

<style lang="scss" scoped>
  .commission {
    padding: 20px;
  }
</style>
